package com.cqeec.dao;

import java.sql.ResultSet;
import java.sql.SQLException;

import com.cqeec.model.Admin;
import com.cqeec.model.User;
import com.cqeec.util.JdbcUtil;
import com.mysql.jdbc.Connection;
import com.mysql.jdbc.PreparedStatement;

/**
 *  登录数据库操作
 * @author 邓学强
 *   密码加密后期改进
 *   1.0
 */
public class LoginDao {
	/**
	 *  管理员登录
	 * @param con 数据库连接
	 * @param admin 管理员实体
	 * @return 查询的结果 
	 */
	public Admin adminLogin(Connection con,Admin admin) {
		String sql="select * from admin where user=?";
		try {
			con=JdbcUtil.getcon();
			PreparedStatement pstm=(PreparedStatement) con.prepareStatement(sql);
			pstm.setString(1, admin.getUser());
			ResultSet rs = pstm.executeQuery();
			if(rs!=null) {
				if(admin.getPassword().trim().equals(rs.getString("password"))) {
					//登录成功
					
				}
				else {
					//登录失败
					
				}
			}
		} catch (ClassNotFoundException | SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return admin;
		
	}
  
	/**
	 * 用户登录界面
	 * @param con 数据库连接
	 * @param user 用户实体
	 * @return  返回查询结果
	 */
	 
	public User userLogin (Connection con,User user) {
		String sql="select * from user where user=?";
		try {
			con=JdbcUtil.getcon();
			PreparedStatement pstm=(PreparedStatement) con.prepareStatement(sql);
			pstm.setString(1, user.getAccount_number());
			ResultSet rs = pstm.executeQuery();
			if(rs!=null) {
				if(user.getPassword().trim().equals(rs.getString("password"))) {
					//登录成功
					
				}
				else {
					//登录失败
					
				}
			}
		} catch (ClassNotFoundException | SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return user;
		
	}

}
